﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QXBook.Utils;
using QXBook.Model;
using QXBook.Service;
using Web.App_Code;

namespace Web.Product
{
    public partial class Product_View : UserBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(CommonUtils.Get("ProductID")))
                {
                    ProductInfo product = ServiceFactory.CreateInstance<ProductService>().GetModel(CommonUtils.ToInt(CommonUtils.Get("ProductID")));
                    this.DataBind(product);
                }
            }

            btnBuy.Click += new EventHandler(btnBuy_Click);
        }

        void btnBuy_Click(object sender, EventArgs e)
        {
            int productID = CommonUtils.ToInt(CommonUtils.Get("ProductID"));
            ProductInfo product = ServiceFactory.CreateInstance<ProductService>().GetModel(productID);
            int nTotal = ServiceFactory.CreateInstance<ShoppingCartService>().InsertToCart(this.userInfo, product);
            Response.Redirect("/Shopping/ShoppingCart.aspx");
        }

        private void DataBind(IModelBase model)
        {
            ProductInfo product = model as ProductInfo;
            if (product != null)
            {
                litTitle.Text = product.Title;
                litPrice.Text = "￥" + product.Price.ToString("0.00");
                litContent.Text = product.Content;

                imgPhoto.ImageUrl = string.IsNullOrEmpty(product.BigImg) ? "/images/nophoto.jpg" : product.BigImg;
                phIsFind.Visible = true;
                phNotFind.Visible = false;

                if (product.IsPublish == YesNo.Yes)
                {
                    phIspublish.Visible = true;
                    phNoPublish.Visible = false;
                }
                else
                {
                    phIspublish.Visible = false;
                    phNoPublish.Visible = true;
                }
            }
            else
            {
                phIsFind.Visible = false;
                phNotFind.Visible = true;
            }
        }
    }
}